package com.fs.base.vo;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import lombok.Data;
import lombok.NoArgsConstructor;

import org.apache.commons.lang3.StringUtils;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.annotation.JSONField;
import com.fs.base.pojo.PkEntity;
import com.fs.base.utils.StringTools;

/**
 * @author TiddlerCJ
 * @desc Combotree控件
 */
@Data
@NoArgsConstructor
public class Combotree {
	/*
	 * 子关系字段
	 */
	@JSONField(serialize=false)
	private String idField;
	/*
	 * 父关系字段
	 */
	@JSONField(serialize=false)
	private String pidField;
	/*
	 * 文本域字段
	 */
	@JSONField(serialize=false)
	private String textField;
	/*
	 * 值域字段
	 */
	@JSONField(serialize=false)
	private String valueField;
	/*
	 * 回显值
	 */
	@JSONField(serialize=false)
	private String textOrValues;
	
	private List<Node> data = new ArrayList<Node>();
	
	public Combotree (List<Node> data){
		this.data = data;
	}
	
	public static <K> Combotree builder(List<K> rows,Combotree tree){
		return builder(rows, tree.getIdField(), tree.getPidField(), tree.getTextField(), tree.getValueField(), tree.getTextOrValues());
	}
	
	public static <K> Combotree builder(List<K> rows,String idField,String pidField,String textField,String valueField,String textOrValues){
		boolean pidFieldNull = (pidField == null);
		List<Node> treeList = new ArrayList<Node>();
		String json = JSON.toJSONString(rows);
		JSONArray array = JSON.parseArray(json);
		String idValue = null ,pidValue = null ,textVauel = null,valueValue = null;
		
		if(StringUtils.isBlank(idField)) idField = PkEntity.PRIMARY_KEY_NAME;
		
		/*
		 * 已经选中或是勾选的记录集合
		 */
		Map<String,String> textOrValuesMap = StringTools.toMap(textOrValues, ","); 
		
		for(int i=0,len=array.size();i<len;i++){
			JSONObject item = array.getJSONObject(i);
			idValue = item.getString(idField);
			textVauel = item.getString(textField);
			valueValue = item.getString(valueField);
			pidValue = pidFieldNull ? null: item.getString(pidField);
			Node tree = new Node(idValue, pidValue,textVauel,valueValue, item);
			treeList.add(tree);
		}
		List<Node> nodeList = new ArrayList<Node>(); 
		for(Node cNode : treeList){  
		    boolean mark = false;   
		    for(Node pNode : treeList){  
		        if(cNode.getPid()!=null && cNode.getPid().equals(pNode.getId())){  
		            mark = true;  
		            if(pNode.getChildren() == null){
		            	pNode.setChildren(new ArrayList<Node>());  
		            }  
		            idValue = cNode.getId();
		            textVauel = cNode.getText();
		            if(textOrValuesMap.containsKey(idValue) || textOrValuesMap.containsKey(textVauel)){
		            	cNode.setChecked(true);cNode.setSelected(true);
		            }
		            pNode.getChildren().add(cNode);   
		            break;  
		        }  
		    }  
		    if(!mark){  
		        nodeList.add(cNode);   
		    }  
		}
		return new Combotree(nodeList) ;
	}
}
